package framelib.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;

import javax.servlet.http.HttpServletResponse;


/**
 * Title:FileUtils
 * Description:
 * @Create_by:Vince Yu
 * @Create_date:2012-5-14
 * @Last_Edit_By:
 * @Edit_Description
 * @version:ShareWithUs 1.0
 * 
 */
public class FileUtils {
	/***
	 * 
	 * 2011-8-24, 下午05:28:32
	 * 方法描述：下载文件 支持所有文件
	 * @param filePathName 文件路径+文件名
	 * @param response
	 * @Create_by 沙创
	 */
	public static void downloadFile(String filePathName,HttpServletResponse response){
		int pos = filePathName.lastIndexOf("/");
		String filepath = filePathName.substring(0, pos+1);
		String filename = filePathName.substring(pos+1);
		downloadFile(filepath, filename, response);
	}
	
	/***
	 * 
	 * 2011-7-27, 下午03:00:46
	 * 方法描述：下载文件 支持所有文件
	 * @param filepath 文件路径
	 * @param filename 文件名
	 * @param response 
	 * @Create_by 沙创
	 */
	public static void downloadFile(String filepath,String filename,HttpServletResponse response)
	{
		OutputStream outputStream = null;
		
		response.setContentType("application/data;charset=UTF-8");
		File file = new File(filepath+filename);
		FileInputStream fis = null;
		byte[] buffer = new byte[4096];
		int bytesRead = 0;
		try {
			outputStream = response.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (file.exists()) {
				response.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes("gb2312"), "ISO8859-1" ));
				response.setHeader("Content-Length", ""+file.length());  
				fis = new FileInputStream(file);
				while ((bytesRead = fis.read(buffer)) != -1) {
					outputStream.write(buffer, 0, bytesRead);
				}
				fis.close();
				outputStream.close();
				
			}
		} catch (Exception ee) {
		}
	}
	
	
	/**
	 * Title:getFileSizes
	 * Description:取得文件大小
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-14
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	public static long getFileSizes(File f) throws Exception{//取得文件大小
        long s=0;
        if (f.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(f);
           s= fis.available();
        }
        return s;
    }
	
	
	/**
	 * Title:FormetFileSize
	 * Description:转换文件大小---M
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-14
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	public static double FormetFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        /*if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "K";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "M";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "G";
        }*/
        return Double.parseDouble(df.format((double) fileS / 1048576));
    }
	
	
}
